package com.kim.weixinProject;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dms.ezplatform.weixin.message.MessageSendInfo;
import com.dms.ezplatform.weixin.message.NewsMessageSendInfo;
import com.kim.weixinProject.util.WxUtils;

import net.sf.json.JSONObject;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@Controller
public class App 
{
	public static final Logger LOGGER = LoggerFactory.getLogger(App.class);
	
	
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
    
   @RequestMapping(value="getToken")
   @ResponseBody
   public JSONObject getToken () {
	   JSONObject jsonObject = new JSONObject();
	    try {
	    	jsonObject.put("token", WxUtils.getAccessToken());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return jsonObject;
   }
   
   @RequestMapping(value="sendMessage")
   @ResponseBody
   public JSONObject sendMessage () {
	    JSONObject jsonObject = new JSONObject();
		this.sendWeiXinMessage();
		jsonObject.put("message","发送成功");
		return jsonObject;
   }
    
    public void sendWeiXinMessage() {
    	String url_start = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa100ddd3da15ff7d&redirect_uri="; 
		String url_end = "&response_type=code&scope=snsapi_base&state=weixin#wechat_redirect";
		String url_middle = "http://weixin.dms365.com:8089/weixin/test";
		
		NewsMessageSendInfo info = new NewsMessageSendInfo();
		List<String> toUserList = new ArrayList<String>();
		toUserList.add("guoyong.guo");
		// 设置接收人
		info.setToUserList(toUserList);
		// 设置消息类型  text:文本消息 news:新闻消息
		info.setMsgtype("news");
		info.setTitle("测试");
		info.setDescription("测试内容11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
				+ "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
				+ "1111111111111111111111111111111111111111111111111111111111111111111");
		// 设置应用ID 可在应用内获取
		info.setAgentid("18");
//		info.setUrl(url_start  + url_middle + url_end);
		Thread thread = new Thread(new WxMsgSend(info));
		thread.start();
    }
    
    class WxMsgSend implements Runnable {
		private MessageSendInfo info;

		public WxMsgSend(MessageSendInfo info) {
			this.info = info;	
		}

		@Override
		public void run() {
			LOGGER.info("微信消息发送，已进入异步线程操作");
			WxUtils.sendMessage(this.info);
			LOGGER.info("微信消息发送，异步线程操作结束");
		}
	}
}
